/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODECPP_DATA_REL_SQLDELAYTHREAD_H_
#define NODECPP_DATA_REL_SQLDELAYTHREAD_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <NodeCpp/Macros.h>
#include <NodeCpp/AutoPtr.h>
#include <NodeCpp/LockedQueue.h>
#include <NodeCpp/Runnable.h>
#include <NodeCpp/Thread.h>
#include <NodeCpp/Mutex.h>
#include <NodeCpp/Event.h>

namespace NodeCpp {
namespace Data {
namespace Rel {
    class Session;
    class SqlOperation;
    class SqlConnection;

    class SqlDelayThread : public Runnable
    {
        typedef LockedQueue<SqlOperation*> SqlQueue;

    public:
        SqlDelayThread(Session* _Sess, SqlConnection* _Conn);
        virtual ~SqlDelayThread(void);

        bool Start(void);
        void Stop(void);
        bool Delay(SqlOperation* _Sql) { SqlQueue_.Add(_Sql); return true; }

        virtual void Run(void);

    private:
        void _ProcessRequests(void);

        SqlQueue SqlQueue_;
        Session* Sess_;
        AutoPtr<SqlConnection> Conn_;
        int ConnPingTime_;
        volatile bool Running_;
        Event StopEvent_;
    };

}}}

#endif // NODECPP_DATA_REL_SQLDELAYTHREAD_H_
